import gui.SWTCodegGUI;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import codeg.Codeg;

public class WorldCodegGUI implements SWTCodegGUI {
    private WorldCodeg codeg;

    public void populate(final Composite content) {
        content.setLayout(new GridLayout(2, false));
        
        Label labelDimensionsField = new Label(content, SWT.NONE);
        labelDimensionsField.setText("Starting position:");
        
        final Label labelDimensions = new Label(content, SWT.NONE);
        labelDimensions.setText(codeg.getWidth() + "x" + codeg.getHeight());
        
        Label lblCor = new Label(content, SWT.NONE);
        lblCor.setText("Color:");
        
        final Canvas canvasColor = new Canvas(content, SWT.NONE);
        canvasColor.setBackground(colorFromCodeg(content.getDisplay(), codeg));
        GridData gd_canvasColor = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
        gd_canvasColor.heightHint = 9;
        canvasColor.setLayoutData(gd_canvasColor);
        
        Button buttonEdit = new Button(content, SWT.NONE);
        buttonEdit.addMouseListener(new MouseAdapter() {
            public void mouseUp(MouseEvent e) {
                WorldDialog wd = new WorldDialog();
                wd.setWidth(codeg.getWidth());
                wd.setHeight(codeg.getHeight());
                wd.setColor(colorFromCodeg(content.getDisplay(), codeg));
                
                wd.run();
                labelDimensions.setText(wd.getWidth() + "x" + wd.getHeight());
                canvasColor.setBackground(wd.getColor());
                
                codeg.setWidth(wd.getWidth());
                codeg.setHeight(wd.getHeight());
                codeg.setColor(colorToCodeg(wd.getColor()));
                codeg.changed();
            }
        });
        buttonEdit.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 2, 1));
        buttonEdit.setText("Edit");
    }
    
    public static Color colorFromCodeg(Display display, WorldCodeg codeg) {
        WorldCodeg.Color c =  codeg.getColor();
        return new Color(display, c.red, c.green, c.blue);
    }
    
    public static WorldCodeg.Color colorToCodeg(Color c) {
        return new WorldCodeg.Color(c.getRed(), c.getGreen(), c.getBlue());
    }
    
    public Codeg getCodeg() {
        return codeg;
    }

    public void setCodeg(Codeg codeg) {
        this.codeg = (WorldCodeg) codeg;
    }
}

class WorldDialog {
    private Color color;
    private int width;
    private int height;
    
    public void run() {
        Display display = Display.getDefault();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        final Composite content = new Composite(shell, SWT.NONE);
        content.setLayout(new GridLayout(5, false));
        
        Label lblDimenses = new Label(content, SWT.NONE);
        lblDimenses.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
        lblDimenses.setText("Dimensions: ");
    
    final Spinner spinnerWidth = new Spinner(content, SWT.BORDER);
    spinnerWidth.setMaximum(1024);
    spinnerWidth.setMinimum(1);
    spinnerWidth.setSelection(getWidth());
    spinnerWidth.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
    
    Label lblPxDeAltura = new Label(content, SWT.NONE);
    lblPxDeAltura.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
    lblPxDeAltura.setText("px in width by");
    
    final Spinner spinnerHeight = new Spinner(content, SWT.BORDER);
    spinnerHeight.setMaximum(1024);
    spinnerHeight.setMinimum(1);
    spinnerHeight.setSelection(getHeight());
    spinnerHeight.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
    
    Label lblPxDeLargura = new Label(content, SWT.NONE);
    lblPxDeLargura.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    lblPxDeLargura.setText("px in height");
    
    Label labelColor = new Label(content, SWT.NONE);
    labelColor.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
    labelColor.setText("Color:");
    
    final Canvas canvasColor = new Canvas(content, SWT.NONE);
    canvasColor.setBackground(getColor());
    GridData gd_canvas = new GridData(SWT.FILL, SWT.FILL, false, false, 3, 1);
    gd_canvas.heightHint = 20;
    canvasColor.setLayoutData(gd_canvas);
    
    Button buttonSelectColor = new Button(content, SWT.NONE);
    buttonSelectColor.addMouseListener(new MouseAdapter() {
        public void mouseUp(MouseEvent e) {
            ColorDialog dialog = new ColorDialog(content.getShell());
            Color color = getColor();
            dialog.setRGB(new RGB(color.getRed(), color.getGreen(), color.getBlue()));
            RGB rgb = dialog.open();
            if (rgb != null)
                canvasColor.setBackground(new Color(Display.getCurrent(), rgb.red, rgb.green, rgb.blue));
        }
    });
    buttonSelectColor.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    buttonSelectColor.setText("Select color");
    
    Button buttonClose = new Button(content, SWT.NONE);
    buttonClose.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 5, 1));
    buttonClose.setText("Close");
        buttonClose.addMouseListener(new MouseListener() {
            public void mouseUp(MouseEvent e) {
                shell.close();
                shell.dispose();
            }
            
            public void mouseDoubleClick(MouseEvent e) {}
            public void mouseDown(MouseEvent e) {}
        });
        shell.addDisposeListener(new DisposeListener() {
            public void widgetDisposed(DisposeEvent e) {
                setWidth(spinnerWidth.getSelection());
                setHeight(spinnerHeight.getSelection());
                setColor(canvasColor.getBackground());
            }
        });        
        content.pack();
        
        shell.open();
        shell.layout();
        shell.pack();
        
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}